package com.cheer.controller;

import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.OaMatrixAddDTO;
import com.cheer.dto.OaMatrixUpdateDTO;
import com.cheer.service.OaMatrixClassService;
import com.cheer.service.OaMatrixService;
import com.cheer.vo.OaMatrixInfoVO;
import com.cheer.vo.OaMatrixListVO;
import com.cheer.vo.SimpleObjectVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/matrix")
public class OaMatrixController {

    @Autowired
    private OaMatrixService oaMatrixService;

    @Autowired
    private OaMatrixClassService oaMatrixClassService;

    @ApiOperation("添加矩阵")
    @PostMapping("/add")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result add(@RequestBody @Valid OaMatrixAddDTO matrixAddDTO) {
        oaMatrixService.add(matrixAddDTO);
        return Result.success();
    }

    @ApiOperation("查询矩阵分类")
    @GetMapping("/class")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<List<SimpleObjectVO>> matrixClass() {
        return Result.success(oaMatrixClassService.listSimple());
    }

    @ApiOperation("查询矩阵列表")
    @GetMapping("/list")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<Page<OaMatrixListVO>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                             @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(oaMatrixService.list(pageNum, pageSize));
    }

    @ApiOperation("删除矩阵")
    @GetMapping("/delete")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result delete(@RequestParam Integer id) {
        oaMatrixService.delete(id);
        return Result.success();
    }

    @ApiOperation("查询矩阵详情")
    @GetMapping("/info")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<OaMatrixInfoVO> info(@RequestParam Integer id) {
        return Result.success(oaMatrixService.info(id));
    }

    @ApiOperation("修改矩阵")
    @PostMapping("/update")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result update(@RequestBody @Valid OaMatrixUpdateDTO matrixUpdateDTO) {
        oaMatrixService.update(matrixUpdateDTO);
        return Result.success();
    }
}
